package com.hzcx.app.simplechat.ui.friend.adapter;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import java.util.ArrayList;
import java.util.List;

public class FriendLableInfoAdapter extends BaseQuickAdapter<FriendBean, BaseViewHolder> {
    public FriendLableInfoAdapter(@Nullable List<FriendBean> data) {
        super(R.layout.rv_item_friend_lable_info_header, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FriendBean item) {
        TextView tvHeader = helper.getView(R.id.tv_header);
        RecyclerView rvContent = helper.getView(R.id.rv_content);

        List<FriendCityBean> list = new ArrayList<>();
        FriendLableInfoContentAdapter contentAdapter = new FriendLableInfoContentAdapter(list);
        rvContent.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        rvContent.setAdapter(contentAdapter);

        contentAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.tv_delete:
                    if (onDeleteListener != null) {
                        onDeleteListener.delete(helper.getAdapterPosition(), position);
                    }
                    break;
            }
        });

        if (item == null || EmptyUtils.isEmpty(item.getName()) || item.getCity() == null || item.getCity().size() <= 0) {
            tvHeader.setVisibility(View.GONE);
            rvContent.setVisibility(View.GONE);
        } else {
            tvHeader.setVisibility(View.VISIBLE);
            rvContent.setVisibility(View.VISIBLE);
            tvHeader.setText(item.getName());
            list.addAll(item.getCity());
            contentAdapter.notifyDataSetChanged();
        }
    }

    public interface OnDeleteListener {
        void delete(int headerPos, int contentPos);
    }

    private OnDeleteListener onDeleteListener;

    public void setOnDeleteListener(OnDeleteListener onDeleteListener) {
        this.onDeleteListener = onDeleteListener;
    }
}
